<?php
namespace app\common\model;
use think\Db;
class Random{

    /**
     * 生成随机数据
     * @param int $type类型  0：充值
     */
    public function create($type=0){
        if(session("?random{$type}"))
            return session("?random{$type}");   #在使用了random之后， 销毁session

        $y = date("Y");
        $m = date("m");
        $d = date("d");
        $today = mktime(0,0,0,$m,$d,$y);
        Db::name('random')->where('type',$type)->where('create_time', '<', $today)->delete();

        #随机数
        $temp = [];
        for($i=0.01;$i<1;$i=$i+0.01){
            $temp[] = $i;
        }
        $rs = Db::name('random')->where('type',$type)->column('money');
        $result = array_diff($temp, $rs);

        $d = [
            'type'        => $type,
            'create_time' => time(),
        ];
        if( !$result ){
            Db::name('random')->where('type',$type)->delete();
            $d['money']=$temp[array_rand($temp)];
        }else{
            $d['money']=$result[array_rand($result)];
        }
        Db::name('random')->insert($d);
        session("random{$type}",$d['money']);
        return $d['money'];
    }
}